import os
import sys
import subprocess

arguments = sys.argv[2]
vdso_usr = os.path.dirname(arguments)
vdso_root = os.path.dirname(vdso_usr)


EXEC_PATH  = os.getenv('RTT_EXEC_PATH') or '/usr/bin'
PREFIX     = os.getenv('RTT_CC_PREFIX') or 'aarch64-none-elf-'

CC      = PREFIX + 'gcc'
CXX     = PREFIX + 'g++'
CPP     = PREFIX + 'cpp'
AS      = PREFIX + 'gcc'
AR      = PREFIX + 'ar'
LINK    = PREFIX + 'gcc'

DEVICE   = ' -march=armv8-a -mtune=cortex-a53 -ftree-vectorize -ffast-math -funwind-tables -fno-strict-aliasing'
CXXFLAGS = DEVICE + ' -Wall -fdiagnostics-color=always'
AFLAGS   = ' -x assembler-with-cpp'
CFLAGS  = DEVICE + ' -Wall -Wno-cpp -std=gnu99 -fdiagnostics-color=always -fPIC -O2'
LFLAGS  = DEVICE + ' -Bsymbolic -Wl,--gc-sections,-u,system_vectors -T {path}/vdso.lds'.format(path=vdso_usr)
CFLAGS += " -I {path} -I{path}/user".format(path=vdso_root)

env = Environment(tools=['gcc', 'link'],
    AS   = AS,   ASFLAGS = AFLAGS,
    CC   = CC,   CFLAGS = CFLAGS,
    CXX  = CXX,  CXXFLAGS = CXXFLAGS,
    AR   = AR,   
    LINK = LINK, LINKFLAGS = LFLAGS)
env.PrependENVPath('PATH', EXEC_PATH)

src = os.path.join(vdso_usr,'vdso_sys.c')
target_name = 'librtos_vdso.so'
target = os.path.join(vdso_usr, "build", target_name)
shared_lib = env.SharedLibrary(target=target, source=src)
env.Default(shared_lib)
